package cc.sunwl.study.java8.异步编程.jdk7;

import java.util.concurrent.*;

public class FutureTest4JDK7 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<Double> future = executorService.submit(new Callable<Double>() {
            @Override
            public Double call() throws Exception {
                long start = System.currentTimeMillis();
                System.out.println("start = " + start);
                Thread.sleep(8000);
                long end = System.currentTimeMillis();
                System.out.println("end = " + end);
                System.out.println("耗时 = " + (end - start));
                return 10D;
            }
        });
        try {
            System.out.println("异步操作进行的同时，不耽误进行其他事情");
            for (int i = 0; i < 10; i++) {
                Thread.sleep(400);
                System.out.println("i = " + i);
            }
            Double result = future.get(3, TimeUnit.SECONDS);
            System.out.println("result = " + result);
        } catch (ExecutionException | InterruptedException | TimeoutException e) {
            System.out.println(e.getMessage());
        }
    }
}
